/* Copyright 2024 Coremail
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * DataStorage，为设置存储于LocalStorage中数据，除了提供appStorage给entry接入，其他方法只对app.ts开放调用
 */
import type { AccountFolder, FolderItem, MailSummary } from './app';
import { FolderData, FolderType, getLogger, MailAttribute } from 'coremail'
const logger = getLogger("DataStorage");

let para: Record<string, string | number | boolean | Array<MailSummary> | Array<AccountFolder>> = {
  'CurrentAccount': '', // 记录当前展示使用账号
  'CurrentFolder': 'INBOX', // 记录当前展示使用文件夹，默认收件箱
  'CurrentFolderType': 1, // 记录当前展示使用文件夹类型，与SDK中FolderType对齐，默认收件箱
  'CurrentFolderUnread': 0, // 记录当前展示使用文件夹未读数，默认0
  'CurrentMailId' : '', // 记录当前读信邮件id
  'CurrentMails' : [], // 记录当前展示使用邮件列表数据
  'CurrentOffsetMid': '', // 记录当前展示使用邮件列表基准偏移邮件mid
  'CurrentReadMailInfoChange' : '', // 记录当前在阅读读信详情的邮件信息更改，值范围：new/flagged/file
  'AccountFolders' : [], // 记录所有账号下的文件夹
  'Searching': false, // 记录当前是否处于搜索状态
  'CurrentSortType': 'date', // 记录当前列表排序方式
  'CurrentSortDesc': true, // 记录当前列表排序是否是降序
};

/**
 * Eml/Msg Viewer界面需要的数据.
 */
let viewerPara: Record<string, string> = {
  'MsgUrl': "",
  'EmlUrl': ""
};
export let appStorage: LocalStorage = new LocalStorage(para);
export let viewerStorage: LocalStorage = new LocalStorage(viewerPara);
export class DataStorage {

  /**
   * 获取当前使用账号
   * @returns
   */
  static currentAccount(): string {
    let accountLink: SubscribedAbstractProperty<string> = appStorage.prop('CurrentAccount')
    return accountLink.get()
  }

  /**
   * 设置LocalStorage存储的当前账号
   * @param email
   * @descripiton 触发条件： 添加账号登录 | 文件夹点击 | 删除账号
   */
  static setCurrentAccount(email: string): void {
    let accountLink: SubscribedAbstractProperty<string> = appStorage.link('CurrentAccount')
    accountLink.set(email)
  }

  /**
   * 获取LocalStorage存储的当前使用文件夹名称
   * @returns
   */
  static currentFolder(): string {
    const folderLink:SubscribedAbstractProperty<string> = appStorage.prop('CurrentFolder')
    return folderLink.get()
  }

  /**
   * 设置LocalStorage存储的当前文件夹名称
   * @param folder 待存储的文件夹名称
   */
  private static setCurrentFolder(folder: string): void {
    const folderLink:SubscribedAbstractProperty<string> = appStorage.link('CurrentFolder')
    folderLink.set(folder)
  }

  /**
   * 获取LocalStorage存储的当前使用文件夹类型
   * @returns
   */
  static currentFolderType(): number {
    const fTypeLink:SubscribedAbstractProperty<number> = appStorage.prop('CurrentFolderType')
    return fTypeLink.get()
  }

  /**
   * 设置LocalStorage存储的当前文件夹类型
   * @param folderType 待存储的文件夹类型
   */
  private static setCurrentFolderType(folderType: number): void{
    const fTypeLink:SubscribedAbstractProperty<number> = appStorage.link('CurrentFolderType')
    fTypeLink.set(folderType)
  }

  /**
   * 更改当前选中的账号和文件夹
   * @param account 待更新的选中账号邮箱
   * @param folder 待更新的选中文件夹
   * @param folderType 待更新的选中文件夹类型
   */
  static changeSelectFolder(account: string, folder: string, folderType: number): void {
    this.setCurrentAccount(account)
    this.setCurrentFolder(folder)
    this.setCurrentFolderType(folderType)
    //更改当前账号的未读数.

  }

  /**
   * 设置LocalStorage存储的当前读信邮件id
   * @param mailId 新邮件id
   */
  static setCurrentMailId(mailId: string): void {
    let mailIdLink: SubscribedAbstractProperty<string> = appStorage.link('CurrentMailId')
    mailIdLink.set(mailId)
  }

  /**
   * 获取LocalStorage存储的当前读信邮件id
   * @returns 当前使用的邮件id
   */
  static currentMailId(): string {
    let mailIdLink: SubscribedAbstractProperty<string> = appStorage.prop('CurrentMailId')
    return mailIdLink.get()
  }

  /**
   * 修改当前读信的标志位
   * @param flagType 标志位类型
   */
  static changeCurrentReadMailFlag(flagType: 'new' | 'flagged'): void{
    DataStorage.readMailInfoChanged(flagType)
  }

  /**
   * 修改当前读信的附件状态
   */
  static currentReadMailAttachmentDownloaded(): void {
    DataStorage.readMailInfoChanged('file')
  }

  /**
   * 当前阅读邮件的信息被发生改变
   * @param infoType 信息类型，取值范围：'new' | 'flagged' | 'file'
   */
  private static readMailInfoChanged(infoType: 'new' | 'flagged' | 'file'): void {
    let flagsLink: SubscribedAbstractProperty<string> = appStorage.link('CurrentReadMailInfoChange')
    flagsLink.set(infoType)
    flagsLink.set('') // 完成后需要清空，否则同个信息类型修改不会触发单向同步
  }

  /**
   * 重置LocalStore存储的邮件列表数据
   * @param mails 新邮件数据
   */
  static resetStoreMails(mails: MailSummary[] , needSort:boolean = false): void {
    let mailsLink:SubscribedAbstractProperty<MailSummary[]> = appStorage.link('CurrentMails')

    if(needSort) {
      mails.sort( (a,b) => {
        return (a.date.getTime() - b.date.getTime()) < 0 ? 1 : -1
      });
    }

    mailsLink.set(mails)
    let offsetLink:SubscribedAbstractProperty<string> = appStorage.link('CurrentOffsetMid')
    if (mails.length > 0) {
      offsetLink.set(mails[mails.length - 1].mid)
    } else {
      offsetLink.set('')
    }
  }

  /**
   * 当前邮件列表删除一封邮件.
   * @param mailId
   */
  static deleteMail(mailId: string): void {
    let mailsLink: SubscribedAbstractProperty<MailSummary[]> = appStorage.link('CurrentMails')
    let currentMails = mailsLink.get()
    const matchIndex = currentMails.findIndex(m => m.mid == mailId)
    if (matchIndex != -1) {
      currentMails.splice(matchIndex, 1);
      mailsLink.set(currentMails)
    }
  }

  /**
   * 往追加LocalStore存储的邮件列表追加数据
   * @param mails 追加的邮件数据
   */
  static appendMails(mails: MailSummary[]): void {
    let mailsLink:SubscribedAbstractProperty<MailSummary[]> = appStorage.prop('CurrentMails')
    let currentMails = mailsLink.get()
    DataStorage.resetStoreMails(currentMails.concat(mails))
  }

  /**
   * 往指定账号文件夹添加新邮件,插入到邮件头部.
   * 需要对mails进行判断,只有比当前数据第一封要新的数据才显示.
   * @param account 账号邮件
   * @param folder 指定文件夹(完整路径)
   * @param mails 待添加的邮件数组
   */
  static addNewMails(account: string, folder: string, mails: MailSummary[],needSort:boolean = false): void {
    // 属于当前展示文件夹，判断排序方式是否合适插入数据
    const currentFolder = this.currentFolder()
    const currentSortType = this.currentSortType()
    const currentSortDesc = this.currentSortDesc()
    const currentAccount = this.currentAccount()

    if (currentAccount == account && currentFolder == folder && currentSortType == 'date' && currentSortDesc == true) {
      let mailsLink:SubscribedAbstractProperty<MailSummary[]> = appStorage.prop('CurrentMails')
      let currentMails = mailsLink.get()
      let currentMinDate = currentMails[currentMails.length-1].date.getTime();
      //目前接收到的邮件的时间可能并不是当前列表最新的,需要过滤,大于此页最小时间的都要显示出来.
      let reallyNewMails = mails.filter((item) => {
        return item.date.getTime() > currentMinDate && currentMails.findIndex((citem)=>(citem.mid == item.mid)) == -1
      })
      DataStorage.resetStoreMails(reallyNewMails.concat(currentMails),needSort)
    }
  }

  /**
   * 邮件标志位发生更新，同步更新LocalStore存储的邮件列表
   * ui界面更新.
   * 从currentMails中,找到匹配的项,更新对应的值,生成新的MailSummary让ui更新.
   * @param mailIds 需要更新的邮件id数组
   * @param flag 更新的标志位类型
   * @param newValue 标志位新值
   * @param newFolder 新的文件夹,如果为null表示采用旧的文件夹不动.
   */
  static updateMails(mailIds: string[], flag: 'new' | 'flagged' | 'folder', newValue: boolean | string): void {
    let mailsLink:SubscribedAbstractProperty<MailSummary[]> = appStorage.link('CurrentMails')
    let currentMails = mailsLink.get()
    for (const mid of mailIds) {
      const matchIndex = currentMails.findIndex(m => m.mid == mid)
      if (matchIndex != -1) {
        const mail = currentMails[matchIndex]
        let newM:MailSummary = {
          mid: mail.mid,
          account: mail.account,
          subject: mail.subject,
          summary: mail.summary,
          date: mail.date,
          hasAttachment: mail.hasAttachment,
          folder: flag == "folder"? newValue as string : mail.folder,
          isNew: flag == 'new' ? newValue as boolean : mail.isNew,
          isFlagged: flag == 'flagged' ? newValue as boolean : mail.isFlagged
        }
        currentMails[matchIndex] = newM
      }
    }
    mailsLink.set(currentMails)
  }

  /**
   * 更新LocalStore存储的邮件列表的邮件摘要
   * @param mailids 待更新的邮件id数组
   * @param summaries 待更新的邮件摘要数组
   */
  static updateSummary(summaryMap: Map<string, string>) {
    let mailsProp:SubscribedAbstractProperty<MailSummary[]> = appStorage.prop('CurrentMails')
    let currentMails = mailsProp.get()
    for (const [mid, summary] of summaryMap) {
      const matchIndex = currentMails.findIndex(m => m.mid == mid)
      if (matchIndex != -1) {
        const mail = currentMails[matchIndex]
        let newM:MailSummary = {
          mid: mail.mid,
          account: mail.account,
          subject: mail.subject,
          summary: summary,
          date: mail.date,
          hasAttachment: mail.hasAttachment,
          folder: mail.folder,
          isNew: mail.isNew,
          isFlagged: mail.isFlagged
        }
        currentMails[matchIndex] = newM
      }
    }
    let mailsLink:SubscribedAbstractProperty<MailSummary[]> = appStorage.link('CurrentMails')
    mailsLink.set(currentMails)
  }

  /**
   * UI列表删除邮件
   * 同步LocalStore存储的邮件列表
   *
   * 会更新当前文件夹的未读数.
   * @param mailIdList 待删除的邮件id数组
   */
  static deleteMails(mailIdList: string[]): void {
    let mailsLink:SubscribedAbstractProperty<MailSummary[]> = appStorage.prop('CurrentMails')
    let currentMails = mailsLink.get()
    let unreadIncrease:number = 0 // 未读数变动增量(正数则未读数增加，负数则未读数减少)
    mailIdList.map(mid => {
      let index = currentMails.findIndex(m => m.mid == mid)
      if (index != -1) {
        if (currentMails[index].isNew) {
          unreadIncrease -= 1
        }
        currentMails.splice(index, 1)
      }
    })
    DataStorage.resetStoreMails(currentMails)
    DataStorage.updateCurrentFolderUnread(unreadIncrease)
    const currentMid = DataStorage.currentMailId()
    if (mailIdList.findIndex(mid => mid == currentMid) != -1) {
      DataStorage.setCurrentMailId('')
    }
  }

  /**
   * 设置LocalStore存储的当前邮件列表排序
   * @param type 排序类型
   * @param desc 是否降序
   */
  static setMailSort(type: 'date'|'size', desc: boolean): void {
    const typeLink:SubscribedAbstractProperty<string> = appStorage.link('CurrentSortType')
    typeLink.set(type)
    const descLink:SubscribedAbstractProperty<boolean> = appStorage.link('CurrentSortDesc')
    descLink.set(desc)
  }

  /**
   * LocalStore存储的当前邮件列表排序类型
   * @returns 排序类型
   */
  static currentSortType(): string {
    const typeLink:SubscribedAbstractProperty<string> = appStorage.prop('CurrentSortType')
    return typeLink.get()
  }


  /**
   * LocalStore存储的当前邮件列表排序是否降序
   * @returns 是否降序
   */
  static currentSortDesc(): boolean {
    const descLink:SubscribedAbstractProperty<boolean> = appStorage.prop('CurrentSortDesc')
    return descLink.get()
  }

  /**
   * 设置LocalStore存储的账号文件夹
   * @param accountFolders 待存储的账号文件夹数组
   */
  static setAccountFolders(accountFolders: AccountFolder[]): void {
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    foldersLink.set(accountFolders)
  }

  /**
   * 从LocalStore存储的账号文件夹中删除指定账号
   * @param account 待删除账号
   */
  static deleteAccountFolders(account: string) : void{
    // 如果当前账号正在读信，先剔除读信记录
    const currentAccount = DataStorage.currentAccount()
    const currentMid = DataStorage.currentMailId()
    if (currentAccount == account && currentMid.length > 0) {
      DataStorage.setCurrentMailId('')
    }
    const foldersLink: SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    let accountFolders = foldersLink.get()
    const index = accountFolders.findIndex(item => item.account == account)
    if (index != -1) {
      accountFolders.splice(index, 1)
    }
    foldersLink.set(accountFolders)
  }

  /**
   * 从指定文件夹目录树中查找匹配路径名字的文件夹
   * @param folder 指定文件夹目录树
   * @param name 待匹配的路径名字
   * @returns 匹配路径名字的文件夹，如果未找到，返回null
   */
  private static findFolderItem(folder: FolderItem, name: string|null): FolderItem|null {
    let subFolders = folder.subFolders
    let names: string[] = []
    let matchFolder: FolderItem|null = null
    if (name) {
      names = name.split("/")
    } else {
      if (folder.name == null || folder.name == "") {
        matchFolder = folder
      }
    }
    while (names.length > 0) {
      let matchName = names[0]
      matchFolder = subFolders.find(f => {
        return f.name == matchName || f.name.endsWith(`/${matchName}`)
      })
      names.splice(0, 1)
      subFolders = matchFolder.subFolders
    }
    return matchFolder
  }

  /**
   * 为LocalStore添加新的文件夹
   * @param account 邮箱账号
   * @param parent 父文件夹名(完整路径名)
   * @param folder 新文件夹
   */
  static addStoreFolder(account: string, parent: string, folder: string): void {
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    let rootFolder = aFolder.rootFolder
    if (rootFolder) {
      let parentFolder: FolderItem|null = DataStorage.findFolderItem(rootFolder, parent)
      parentFolder.subFolders.push({name: folder, unreadCount: 0, subFolders: [], type: FolderType.other})
      aFolder.rootFolder = rootFolder
      const jsonStr = JSON.stringify(accountFolders)
      const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
      foldersLink.set(newFolders)
    }
  }

  /**
   * 更新LocalStore存储的文件夹名字
   * @param account 邮箱账号
   * @param folder 当前文件夹名字(完整路径名)
   * @param newName 新文件夹名字(完整路径名)
   */
  static renameStoreFolder(account: string, folder: string, newName: string): void {
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    let rootFolder = aFolder.rootFolder
    if (rootFolder) {
      let f = DataStorage.findFolderItem(rootFolder, folder)
      f.name = newName
      const jsonStr = JSON.stringify(accountFolders)
      const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
      foldersLink.set(newFolders)
    }
  }

  /**
   * 从LocalStore中删除文件夹
   * @param account 账号邮箱
   * @param parent 父文件夹(完整路径名)
   * @param folder 待删除文件夹
   */
  static deleteStoreFolder(account: string, parent: string, folder: string): void {
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    let rootFolder = aFolder.rootFolder
    if (rootFolder) {
      let parentF = DataStorage.findFolderItem(rootFolder, parent)
      const index = parentF.subFolders.findIndex(item => item.name == folder)
      parentF.subFolders.splice(index, 1)
      aFolder.rootFolder = rootFolder
      const jsonStr = JSON.stringify(accountFolders)
      const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
      foldersLink.set(newFolders)
    }
  }

  /**
   * 更新LocalStore中文件夹的未读数
   * @param account 账号邮箱
   * @param folder 文件夹(完整路径名)
   * @param increaseCount 未读数增量(正数则未读数增加，负数则未读数减少)
   *
   */
  static updateStoreFolderUnread(account: string, folder: string, increaseCount: number): void{
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    let rootFolder = aFolder.rootFolder
    if (rootFolder) {
      let f = DataStorage.findFolderItem(rootFolder, folder)
      f.unreadCount += increaseCount
      const jsonStr = JSON.stringify(accountFolders)
      const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
      foldersLink.set(newFolders)
    }

    const currentFolderLink:SubscribedAbstractProperty<string> = appStorage.link('CurrentFolder')
    const currentFolder = currentFolderLink.get()
    if (folder == currentFolder) {
      this.updateCurrentFolderUnread(increaseCount)
    }
  }


  /**
   * 仅更新文件夹树中对应未读数的数量.
   * @param account
   * @param folder
   * @param increaseCount
   */
  static updateFolderTreeUnreadBySet(account: string, folder: string, count:number): void{
    let map:Map<string,number> = new Map();
    map.set(folder,count);
    DataStorage.updateFolderTreeUnreadsBySet(account,map);
  }

  //
  static updateFolderTreeUnreadsBySet(account: string, map:Map<string,number>): void{
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    let rootFolder = aFolder.rootFolder
    if (rootFolder) {
      map.forEach((count,folder)=>{
        let f = DataStorage.findFolderItem(rootFolder, folder)
        f.unreadCount = count;
      })
    }
    const jsonStr = JSON.stringify(accountFolders)
    const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
    foldersLink.set(newFolders)
  }

  static currentDisplayMailCount() {
    let mailsLink: SubscribedAbstractProperty<MailSummary[]> = appStorage.link('CurrentMails')
    return mailsLink.get().length;
  }

  static isNew(attrs: MailAttribute[]): boolean {
    return attrs.findIndex((item) => (item == MailAttribute.Seen)) == -1
  }

  static isFlag(attrs: MailAttribute[]): boolean {
    return attrs.findIndex((item) => (item == MailAttribute.Flagged)) != -1
  }

  /**
   * 邮件标志位发生更新，同步更新LocalStore存储的邮件列表
   * ui界面更新.
   * 从currentMails中,找到匹配的项,更新对应的值,生成新的MailSummary让ui更新.
   * @param mailIds 需要更新的邮件id数组
   * @param flag 更新的标志位类型
   * @param newValue 标志位新值
   * @param newFolder 新的文件夹,如果为null表示采用旧的文件夹不动.folder变化了,应该删除该文件吧???
   */
  static updateMailAttr(map: Map<string, MailAttribute[]>): void {
    let mailsLink: SubscribedAbstractProperty<MailSummary[]> = appStorage.link('CurrentMails')
    let currentMails = mailsLink.get()
    let updatedMails:number = 0;
    map.forEach((attrs, mailId) => {
      const matchIndex = currentMails.findIndex(m => m.mid == mailId)
      if (matchIndex != -1) {
        updatedMails++;
        const mail = currentMails[matchIndex]
        let isNew = DataStorage.isNew(attrs);
        logger.debug(`[UpdateMailAttr][id:${mail.mid} subject:${mail.subject} isNew:${isNew},index:${matchIndex}]`)
        let newMail = Object.assign({}, mail);
        newMail.isNew = isNew;
        currentMails[matchIndex] = newMail
        newMail.isFlagged = DataStorage.isFlag(attrs);
      }
    })
    if(updatedMails > 0) {
      logger.debug(`[UpdateCurrentMails,update ${updatedMails} mails]`)
      mailsLink.set(currentMails)
    }
  }

  /**
   * 更新一个账号的文件夹树.
   * @param account
   * @param rootFolder
   */
  static updateAccountFolderTree(account: string,rootFolder:FolderItem): void{
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    aFolder.rootFolder = rootFolder
    const jsonStr = JSON.stringify(accountFolders)
    const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
    foldersLink.set(newFolders)
  }

  /**
   * 设置LocalStore中文件夹的未读数
   * @param account 账号邮箱
   * @param folder 指定文件夹(完整路径)
   * @param unreadCount 新未读数量
   */
  static setStoreFolderUnread(account: string, folder: string, unreadCount: number): void {
    const foldersLink:SubscribedAbstractProperty<AccountFolder[]> = appStorage.link('AccountFolders')
    const accountFolders = foldersLink.get()
    let aFolder = accountFolders.find(item => item.account == account)
    let rootFolder = aFolder.rootFolder
    if (rootFolder) {
      let f = DataStorage.findFolderItem(rootFolder, folder)
      f.unreadCount = unreadCount
      const jsonStr = JSON.stringify(accountFolders)
      const newFolders: AccountFolder[] = JSON.parse(jsonStr) as AccountFolder[]
      foldersLink.set(newFolders)
    }

    const currentFolderLink:SubscribedAbstractProperty<string> = appStorage.link('CurrentFolder')
    const currentFolder = currentFolderLink.get()
    if (folder == currentFolder) {
      this.setCurrentFolderUnread(unreadCount)
    }
  }

  /**
   * 设置LocalStore存储的当前展示使用文件夹未读数
   * @param count
   */
  static setCurrentFolderUnread(count: number): void {
    const countLink:SubscribedAbstractProperty<number> = appStorage.link('CurrentFolderUnread')
    countLink.set(count)
  }

  /**
   * 更新LocalStore存储的当前展示使用文件夹未读数
   * @param increaseCount 未读数增量(正数则未读数增加，负数则未读数减少)
   */
  static updateCurrentFolderUnread(increaseCount: number): void {
    const countLink:SubscribedAbstractProperty<number> = appStorage.link('CurrentFolderUnread')
    const count = countLink.get()
    countLink.set(count + increaseCount)
  }

  /**
   * 更新LocalStore存储的当前展示使用文件夹未读数
   * @param count 未读数.
   */
  static updateCurrentFolderUnreadBySet(count: number): void {
    const countLink:SubscribedAbstractProperty<number> = appStorage.link('CurrentFolderUnread')
    countLink.set(count)
  }

  /**
   * 设置LocalStore记录的当前搜索状态
   * @param searching 待存储的搜索状态
   * @discuss 搜索后点击搜索结果列表读信，不应设置CurrentMailId，否则如果返回到主页面进行删除系列操作，会导致相关的错误
   */
  static setSearching(searching: boolean): void {
    const searchLink:SubscribedAbstractProperty<boolean> = appStorage.link('Searching')
    searchLink.set(searching)
  }

  /**
   * 获取LocalStore记录的当前搜索状态
   * @returns 当前搜索状态
   */
  static isSearching(): boolean {
    const searchLink:SubscribedAbstractProperty<boolean> = appStorage.prop('Searching')
    return searchLink.get()
  }

  /**
   * 设置viewer的url数据.
   * @param url
   * @param isEml
   * true: eml文件
   * false:msg文件
   */
  static setViewerUrl(url: string, isEml: boolean): void {
    if(isEml) {
      const link:SubscribedAbstractProperty<string> = viewerStorage.link('EmlUrl')
      link.set(url);
    }
    else {
      const link:SubscribedAbstractProperty<string> = viewerStorage.link('MsgUrl')
      link.set(url);
    }
  }
}
